﻿// By Conmajia<conmajia@gmail.com> on September 6th., 2012
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CSharpGo
{
    public class Game
    {
        string name = string.Empty;
        int bottleCount = 0;
        bool initiated = false;
        int targetBottle = -1;
        int targetAmount = -1;

        World world;

        Random rand = new Random();

        public string Name
        {
            get { return name; }
        }

        public int Target
        {
            get { return targetBottle; }
        }

        public int Amount
        {
            get { return targetAmount; }
        }

        public World World
        {
            get { return world; }
        }

        public Game()
        {
            world = new World();
        }

        /// <summary>
        /// Difficaulty of game.
        /// </summary>
        /// <param name="difficaulty">Difficaulty from 1 to 3.</param>
        public void AutoGenerate(int difficaulty)
        {
            if (difficaulty < 1)
                return;

            world.DestroyBottle();

            int bottleCount = rand.Next(3, 5); //3 + difficaulty);
            targetBottle = rand.Next(0, bottleCount - 1);

            int maxAmount = 10;
            for (int i = 0; i < bottleCount; i++)
            {
                int cap = 0;
                do
                {
                    cap = rand.Next(3, maxAmount + difficaulty);
                } while (capacityInside(cap));

                world.CreateBottle(cap);
            }

            targetAmount = rand.Next(1, world.Bottles[targetBottle].Capacity);

            initiated = true;
        }

        bool capacityInside(int cap)
        {
            foreach (Bottle b in world.Bottles)
            {
                if (b.Capacity == cap)
                    return true;
            }

            return false;
        }

        public bool CheckSuccess()
        {
            if (targetBottle > -1)
            {
                if (initiated && world.Bottles.Count > targetBottle)
                {
                    return world.Bottles[targetBottle].Content == targetAmount;
                }
            }

            return false;
        }
    }
}
